<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":"
            + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />

    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css" />
    <title>添加订单商品</title>
</head>
<body>
<article class="page-container">
    <form class="form form-horizontal" action="${empty orderDetail ? 'order/insertDetail':'order/updateDetail'}" method="post" id="form-admin-add">

        <input type="hidden" ${not empty orderDetail?'':'disabled'} value="${orderDetail.orderDetailId}" id="orderDetailId" name="orderDetailId">

        <input type="hidden" value="${empty orderId?orderDetail.orderId:orderId}" id="orderId" name="orderId">

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>商品名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="${orderDetail.goodsName}" placeholder="商品名称" id="goodsName" name="goodsName">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>商品数量：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="${orderDetail.goodsNumber}" placeholder="商品数量" id="goodsNumber" name="goodsNumber">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">计量单位：</label>
            <div class="formControls col-xs-8 col-sm-9"> <span class="select-box" style="width:150px;">
			<select class="select" id="goodsUnit" name="goodsUnit" size="1">
				<option value="-1">-请选择-</option>
                <c:forEach items="${units}" var="unit">
                    <option value="${unit.baseId}" ${orderDetail.goodsUnit == unit.baseId ?'selected':''}>${unit.baseName}</option>
                </c:forEach>
			</select>
			</span> </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>商品单价/元：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="${orderDetail.goodsUnitPrice}" placeholder="商品单价/元" id="goodsUnitPrice" name="goodsUnitPrice">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">商品总价/元：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="hidden" value="${orderDetail.goodsTotal}" id="goodsTotal" name="goodsTotal">
                <span id="totalPrice">￥${orderDetail.goodsTotal}</span>
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">商品说明：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <textarea name="goodsRemark" id="goodsRemark" class="textarea"  placeholder="请输入商品说明" dragonfly="true">${orderDetail.goodsRemark}</textarea>
            </div>
        </div>


        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
    var reg=/^[0-9]+$/;
    function calculator(){
        var num=$("#goodsNumber").val();
        var price=$("#goodsUnitPrice").val();
        if (reg.test(num) && reg.test(price)){
            var total=parseInt(num)*parseInt(price);
            $("#goodsTotal").val(total);
            $("#totalPrice").html("￥"+total);
        }
    }
    $("#goodsNumber").blur(function () {
        calculator()
    });
    $("#goodsUnitPrice").blur(function () {
        calculator()
    });

    $(function () {
        <c:if test="${not empty flag && flag==1}">
            var orderDetail=parent.getDetailInfo();
            if (orderDetail!=null){
                $("#orderDetailId").val(orderDetail.orderDetailId);
                $("#orderId").val(orderDetail.orderId);
                $("#goodsName").val(orderDetail.goodsName);
                $("#goodsNumber").val(orderDetail.goodsNumber);
                var units=$("#goodsUnit option");
                for (var i = 0; i < units.length; i++) {
                    if ($(units[i]).val()==orderDetail.goodsUnit){
                        units[i].selected=true;
                        break;
                    }
                }
                $("#goodsUnitPrice").val(orderDetail.goodsUnitPrice);
                $("#goodsTotal").val(orderDetail.goodsTotal);
                $("#totalPrice").html("￥"+orderDetail.goodsTotal);
                $("#goodsRemark").val(orderDetail.goodsRemark);
            }
        </c:if>
    })
    
    $(function(){
        $("#form-admin-add").validate({
            rules:{
                goodsName:{
                    required:true
                },
                goodsNumber:{
                    required:true,
                    isDigits:true,
                    min:1
                },
                goodsUnit:{
                    min:0
                },
                goodsUnitPrice:{
                    required:true,
                    isDigits:true,
                    min:1
                }
            },
            messages:{
                goodsName:{
                    required:"商品名称不能为空"
                },
                goodsNumber:{
                    required:"商品数量不能为空",
                    isDigits:"商品数量只能是数字",
                    min:"商品数量不能少于1"
                },
                goodsUnit:{
                    min:"请选择单位"
                },
                goodsUnitPrice:{
                    required:"单价不能为空",
                    isDigits:"商品单价只能是数字",
                    min:"商品单价不能少于1"
                }
            },
            submitHandler:function(form){
                <c:if test="${not empty flag}">
                    var orderDetailId=$("#orderDetailId").val();
                    var orderId=$("#orderId").val();
                    var goodsName=$("#goodsName").val();
                    var goodsNumber=$("#goodsNumber").val();
                    var goodsUnit=$("#goodsUnit").find("option:selected").val();
                    var goodsUnitPrice=$("#goodsUnitPrice").val();
                    var goodsTotal=$("#goodsTotal").val();
                    var goodsRemark=$("#goodsRemark").val();

                    var orderDetail={
                        orderDetailId:orderDetailId,
                        orderId:orderId,
                        goodsName:goodsName,
                        goodsNumber:goodsNumber,
                        goodsUnit:goodsUnit,
                        goodsUnitPrice:goodsUnitPrice,
                        goodsTotal:goodsTotal,
                        goodsRemark:goodsRemark
                    };
                    var msg='';
                    <c:if test="${flag==1}">
                        parent.updateDetailInfo(orderDetail);
                        msg="商品信息已修改,提交订单后生效";
                    </c:if>
                    <c:if test="${flag==2}">
                        parent.addDetailInfo(orderDetail);
                        msg="新商品信息已添加,提交订单后生效";
                    </c:if>

                    layer.msg(msg,{icon:1,time:1000},function () {
                        parent.layer.closeAll();
                    });

                </c:if>

                <c:if test="${empty flag}">
                    $(form).ajaxSubmit(function(res){
                        layer.msg(res.msg,{icon:res.code,time:1000},function () {
                            if (1==res.code){
                                parent.refreshSubTable();
                                parent.layer.closeAll();
                            }
                        });
                    });
                </c:if>
            }
        })
    });
</script>
</body>
</html>